這 開放電信平台(OTP) 是一套強大的 Erlang 庫和設計原則,用以規範演員模型。它提供了被稱為「藍圖」的 行為,讓開發者能透過抽象化流程管理,建立分散式且具容錯能力的應用程式。
1. OTP 結構
OTP 是一個包含 Erlang、 Mnesia 資料庫,以及標準化的應用程式架構 應用程式。一個應用程式由遵循嚴格 OTP 慣例(行為)的流程組成。
2. 行為作為合約
行為是業界標準的通用模式範本: GenServer 適用於通用伺服器,訊息處理的事件監聽器,以及複雜邏輯的狀態機。
3. I/O 作為流程通訊
在 Erlang 虛擬機中,I/O 操作由 I/O 伺服器執行。這些是實作低階訊息介面的流程,可透過簡單通訊將輸出重導至遠端節點的群組領導者。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What does OTP stand for in the context of Elixir and Erlang?
Open Transmission Protocol
Open Telecom Platform
Operational Task Processor
Optimized Threading Portal
✅ Correct!
Correct! OTP is the Open Telecom Platform, providing the framework for distributed Elixir apps.❌ Incorrect
OTP specifically refers to the Open Telecom Platform stack.QUESTION 2
In OTP, what is a 'Behavior'?
A specific way a programmer types code.
A runtime error logging mechanism.
A standard convention or contract for process patterns.
A module that handles database migrations.
✅ Correct!
Correct. Behaviors provide generic code for patterns like servers, requiring only specific business logic from the dev.❌ Incorrect
Behaviors act as contracts for common patterns like GenServers or Event Handlers.QUESTION 3
How are input and output (I/O) performed in the Erlang VM?
Direct CPU system calls.
Via I/O servers, which are just Erlang processes.
Through a static global file registry.
By writing directly to hardware registers.
✅ Correct!
True! This allows Elixir to treat I/O as message passing, enabling remote node output control.❌ Incorrect
I/O in the BEAM is abstracted through I/O server processes.QUESTION 4
What is included in the OTP bundle besides the Erlang runtime?
Only the Elixir compiler.
A database (Mnesia) and numerous libraries.
A web browser and a text editor.
The Python interpreter.
✅ Correct!
OTP includes Erlang, Mnesia, and a massive library of design patterns.❌ Incorrect
OTP is a comprehensive bundle of tools including Mnesia and standard libraries.QUESTION 5
What does an OTP 'Application' consist of?
A single executable file.
One or more processes following OTP conventions.
A collection of static HTML files.
A set of functions without processes.
✅ Correct!
OTP Applications are groups of processes organized into supervision trees.❌ Incorrect
In OTP, applications are structural units composed of managed processes.Case Study: The Distributed Ticker
Inter-node communication using OTP logic
You have two nodes: 'one@light-boy' and 'two@light-boy'. You want to send a 'tick' message from Node One and have Node Two respond to it, while also printing output back to Node One's console.
Q
1. How do you identify a process on Node Two from Node One without knowing its dynamic PID?
Solution:
Use
Use
:global.register_name(:name, pid) on Node Two, and :global.whereis_name(:name) on Node One to retrieve the PID.Q
2. If Node One executes 'IO.puts(remote_pid, "Hello")', where does 'Hello' appear and why?
Solution:
It appears on Node One's terminal if
It appears on Node One's terminal if
remote_pid points to a process that has its group leader set to the local node's I/O server. This demonstrates I/O as process messaging.Q
3. Why does the Ticker module use 'spawn' in its 'start' function in this context?
Solution:
It creates a separate process to run the 'receiver' loop, allowing the main Ticker module to remain responsive while the receiver waits for messages.
It creates a separate process to run the 'receiver' loop, allowing the main Ticker module to remain responsive while the receiver waits for messages.